package com.sjy.ylxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.domain.YlxtAppointmentDetails;
import com.sjy.ylxt.entity.domain.YlxtComplaints;
import com.sjy.ylxt.entity.dto.AddAppointmentDTO;
import com.sjy.ylxt.entity.dto.FeedbackAppointmentDTO;
import com.sjy.ylxt.entity.dto.QueryAppointmentsDTO;
import com.sjy.ylxt.enums.AppointmentStatus;
import com.sjy.ylxt.enums.DeleteFlagEnum;
import com.sjy.ylxt.factory.O2O;
import com.sjy.ylxt.mapper.YlxtAppointmentDetailsMapper;
import com.sjy.ylxt.mapper.YlxtComplaintsMapper;
import com.sjy.ylxt.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sjy.ylxt.entity.domain.YlxtAppointments;
import com.sjy.ylxt.mapper.YlxtAppointmentsMapper;
import com.sjy.ylxt.service.YlxtAppointmentsService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* @Author: 史俊颖
* @CreateTime: 2024-12-12
* @Description: ${description}
*/
@Service
@Slf4j
public class YlxtAppointmentsServiceImpl extends ServiceImpl<YlxtAppointmentsMapper, YlxtAppointments> implements YlxtAppointmentsService{

    @Resource
    private YlxtAppointmentsMapper ylxtAppointmentsMapper;

    @Resource
    private YlxtComplaintsMapper ylxtComplaintsMapper;
    @Autowired
    private YlxtAppointmentDetailsMapper ylxtAppointmentDetailsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ylxtAppointmentsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(YlxtAppointments record) {
        return ylxtAppointmentsMapper.insert(record);
    }

    @Override
    public int insertSelective(YlxtAppointments record) {
        return ylxtAppointmentsMapper.insertSelective(record);
    }

    @Override
    public YlxtAppointments selectByPrimaryKey(Integer id) {
        return ylxtAppointmentsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(YlxtAppointments record) {
        return ylxtAppointmentsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(YlxtAppointments record) {
        return ylxtAppointmentsMapper.updateByPrimaryKey(record);
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public ResultBody<?> pageList(QueryAppointmentsDTO dto) {
        try {
            log.info("==> 开始分页查询预约列表");
            // 设置分页信息
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
            // 构建查询条件
            LambdaQueryWrapper<YlxtAppointments> qw = new LambdaQueryWrapper<>();
            if (dto.getPatientId() != null && !dto.getPatientId().isEmpty()) {
                qw.eq(YlxtAppointments::getPatientId, dto.getPatientId());
            }
            if (dto.getCompanionId() != null && !dto.getCompanionId().isEmpty()) {
                qw.eq(YlxtAppointments::getCompanionId, dto.getCompanionId());
            }
            if (dto.getAppointmentDate() != null && !dto.getAppointmentDate().isEmpty()) {
                qw.eq(YlxtAppointments::getAppointmentDate, dto.getAppointmentDate());
            }
            if (dto.getStatus() != null) {
                qw.eq(YlxtAppointments::getStatus, dto.getStatus());
            }
            qw.eq(YlxtAppointments::getDeleteFlag, DeleteFlagEnum.UN_DELETE_FLAG.getCode());
            // 执行查询
            PageInfo<YlxtAppointments> pageInfo = new PageInfo<>(ylxtAppointmentsMapper.selectList(qw));
            return ResultBody.success(pageInfo);
        } catch (Exception e) {
            log.error("分页查询预约列表失败", e);
            return ResultBody.error("分页查询预约列表失败");
        }
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public ResultBody<?> addAppointments(AddAppointmentDTO dto) {
        try {
            log.info("==> 开始预约");
            // 插入主表 YlxtAppointments 记录
            YlxtAppointments appointments = new YlxtAppointments();
            YlxtAppointments ylxtAppointments = O2O.AppointmentDTOToEntity(dto, appointments);
            int i = new Random().nextInt(100000);
            ylxtAppointments.setId(Long.valueOf(i));
            ylxtAppointments.setDeleteFlag(DeleteFlagEnum.UN_DELETE_FLAG.getCode());
            ylxtAppointments.setCreateTime(new Date());
            ylxtAppointments.setStatus(AppointmentStatus.UNRESERVED.getCode());
            ylxtAppointmentsMapper.insert(ylxtAppointments);
            // 插入子表 YlxtAppointmentDetails 记录
            YlxtAppointmentDetails details = new YlxtAppointmentDetails();
            details.setId(SnowFlakeUtil.getInstance().nextId()); // 使用雪花算法生成唯一ID
            details.setAppointmentId(ylxtAppointments.getId()); // 关联到主表的ID
            details.setServiceType(dto.getServiceType());
            details.setLocation(dto.getLocation());
            details.setDescription(dto.getDescription());
            details.setEvaluationScore(null); // 初始化为 null 或其他默认值
            details.setEvaluationComment(null); // 初始化为 null 或其他默认值
            details.setCreateTime(new Date());
            details.setUpdateTime(new Date());
            details.setCreateUser("admin"); // 设置创建用户，这里假设为 "admin"
            details.setUpdateUser("admin"); // 设置修改用户，这里假设为 "admin"
            details.setDeleteFlag(DeleteFlagEnum.UN_DELETE_FLAG.getCode());
            ylxtAppointmentDetailsMapper.insert(details);
            log.info("==> 预约成功");
            return ResultBody.success();
        } catch (Exception e) {
            log.error("==> 添加预约失败, 报错信息为: {}", e.getMessage(), e);
            return ResultBody.error("添加预约失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ResultBody<?> complaints(String id, String complaintContent) {
        try{
            log.info("==> 开始投诉");
            YlxtAppointments ylxtAppointments = ylxtAppointmentsMapper.selectById(id);
            YlxtComplaints record = new YlxtComplaints();
            record.setPatientId(ylxtAppointments.getPatientId());
            record.setCompanionId(ylxtAppointments.getCompanionId());
            record.setComplaintContent(complaintContent);
            record.setComplaintDate(new Date());
            ylxtComplaintsMapper.insert(record);
            log.info("==> 投诉成功");
            return ResultBody.success();
        }catch (Exception e){
            log.error("==> 投诉失败, 报错信息为: {}", e.getMessage(), e);
            return ResultBody.error("投诉失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ResultBody<?> cancelAppointments(String id) {
        try{
            log.info("==> 开始取消预约");
            YlxtAppointments ylxtAppointments = ylxtAppointmentsMapper.selectById(id);
            ylxtAppointments.setStatus(AppointmentStatus.CANCELLED.getCode());
            ylxtAppointmentsMapper.updateById(ylxtAppointments);
            log.info("==> 取消预约成功");
            return ResultBody.success();
        }catch (Exception e){
            return ResultBody.error("取消预约成功");
        }
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public ResultBody<?> feedback(FeedbackAppointmentDTO dto) {
        try{
            log.info("==> 开始评价反馈");
            YlxtAppointments ylxtAppointments = ylxtAppointmentsMapper.selectById(dto.getId());
            ylxtAppointments.setStatus(AppointmentStatus.COMPLETED.getCode());
            ylxtAppointmentsMapper.updateById(ylxtAppointments);

            YlxtAppointmentDetails details = ylxtAppointmentDetailsMapper.selectById(dto.getId());
            details.setEvaluationScore(dto.getEvaluationScore());
            details.setEvaluationComment(dto.getEvaluationContent());
            ylxtAppointmentDetailsMapper.updateById(details);
            log.info("==> 评价反馈成功");
            return ResultBody.success();
        }catch (Exception e){
            return ResultBody.error("评价反馈成功");
        }
    }

}
